package List2;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-04-06
 * Time:20:16
 */
public class MyList2 <T>{
    public int size;
    private class List{
        //	存储数据
        T item;
        //	下一个结点
       List next;
        public List(T item) {
            this.item = item;
        }
        public List() {};
    }
    private List head;
    /* public void creatList()
     {
         List kk=new List(12);
         List kk1=new List(13);
         List kk2=new List(14);
         List kk3=new List(15);
       kk.next=kk1;
       kk1.next=kk2;
       kk2.next=kk3;
       kk3=null;
       this.head=kk;
     }*/
    public int size(){
        List cur=head;
        int size=0;
        while(cur!=null)
        {
            size++;
            cur=cur.next;
        }
        return size;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(T key){
        List cur=head;
        while(cur!=null)
        {
            if(cur.item==key)
                return true;

        }
        return false;
    }

    //头插法
    public void inserthead(T sum)
    {
        List new1=new List(sum);
        new1.next=head;
        head=new1;
        size++;
    }
    //尾插法
    public void insertTail(T sum) {
      List cur = head;
        List new1 = new List(sum);
        while (cur.next != null) {
            cur =cur.next;
        }
        cur.next=new1;
        new1.next=null;
    }

    //展示
    public void Show()
    {
       List cur=head;
        while(cur!=null)
        {
            System.out.print(cur.item+" ");
            cur =cur.next;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,T data){
        if(index<0||index>size-1)
        {
            System.out.println("非法");
            return;
        }
        List cur=head;
        int i=0;
        while(i<index-1)
        {
            cur=cur.next;
            i++;
        }
        List fw=new List(data);
        fw.next=cur.next;
        cur.next=fw;
    }
}

